package com.mediastory.spsserver.adCategory.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mediastory.spsserver.adCategory.service.AdCategorySvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/adCategory"})
public class AdCategoryCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private AdCategorySvc adCategorySvc;

	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		Integer adCategoryIdx = ServletRequestUtils.getIntParameter(request, "adCategoryIdx");
		condition.put("adCategoryIdx", adCategoryIdx);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("adCategoryList", adCategorySvc.findbyList(null));
		
		return resultMap;
	}
	
	/**
	 * 카테고리 내외 순서 목록 json
	 * */
	@RequestMapping(method=RequestMethod.GET, value="/dispOrderList/{adCategoryIdx}", headers="Accept=application/json")
	public @ResponseBody Map<String, Object> getDispOrderList(@PathVariable Integer adCategoryIdx, HttpSession session) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap.put("resultCode", "200");
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("adCategoryIdx", adCategoryIdx);
			resultMap.put("inCont", adCategorySvc.findbyListInCont(condition));
			resultMap.put("outCont", adCategorySvc.findbyListOutCont(condition));
		} catch (Exception e) {
			// TODO: handle exception
			//e.printStackTrace();
			resultMap.put("resultCode", "500");
			resultMap.put("errMsg", "Error");
		}

		return resultMap;
	}

	/**
	 * 카테고리 순서 등록/수정
	 * */
	@RequestMapping(value={"/dispOrder/submit.do"}, method=RequestMethod.POST)
	public String submit(HttpServletRequest request, HttpSession session) throws Exception {
		int adCategoryIdx = ServletRequestUtils.getRequiredIntParameter(request, "adCategoryIdx");
		int[] dispOrderList = ServletRequestUtils.getIntParameters(request, "dispOrder");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("adCategoryIdx", adCategoryIdx);
		condition.put("dispOrderList", dispOrderList);
		
		adCategorySvc.addDispOrder(condition);
		
		return "redirect:/adCategory/list.do?adCategoryIdx="+adCategoryIdx;
	}
	
}